use  "${data1}march_regready_1984.dta", clear
replace contpov = contpov2
replace eitc = eitc*eitc_ref
gen byte demgroup_lowskill = demgroup26==1 & inrange(age, 16,24) 
gen byte demgroup_workingage = inrange(age, 16, 64)
gen byte demgroup_children = age<16
ren (demgroup0 demgroup_workingage demgroup27  demgroup_lowskill demgroup21 demgroup24  demgroup_children) ///
(demgroup2 demgroup3 demgroup4 demgroup5 demgroup6 demgroup7 demgroup8)


cap program drop mydisp
program mydisp, eclass
         matrix betass=e(b)
         matrix colnames betass = "E"
         mat list betass
         ereturn repost b = betass, rename
 end
*
*
* local stat summstat(N)
* local stat noau


local append outreg, merge(r) starloc(1) var stats(b se) blankrow  merge starlevels(10 5 1) bdec(4) noau  keep(lnMW ) nolegend
local append1 outreg, merge(r) starloc(1) var stats(b se) blankrow  merge starlevels(10 5 1) bdec(4) noau   nolegend


local spec 4
local j .5
local d 1
outreg, clear
local thresholdlist 0.50(0.25)1.75
local thresholdlist .5  1 1.5 2 3 4
foreach j of numlist `thresholdlist' {
cap drop under
g  under = contpov < `j'
local q = `j' * 100
di "Poverty threshold `q'"
label var lnMW "INR<`j'"
local i 1
sum under [aw=wtsupp] if demgroup1==1
local DVmean=   `r(mean)'
di `DVmean'

* reghdfe under lnMWF1 lnMW lnMWL1 lnMWL2 lnMWL3 `primary'  [pw=fnlwgt] , a(`rhs`i'') cluster(state_fips)
* outreg, store(r)  starloc(1) replace var stats(b se )  blankrow starlevels(10 5 1) bdec(3) `stat' keep(tobacco21_treated  ) nolegend  addrow("","") ct("","(1)"   )
estimates use "${est}ster/dube/MW1_`q'_dem1_s`spec'_dube"
  if `j' == 4   local obs : display %11.0gc `e(N)'
*   di `obs'
  else local obs
  local beta = _b[lnMW]
  di `beta'
  local elas:  di %5.4f `beta'/`DVmean'
  di `elas'
  outreg, store(r)  starloc(1) replace var stats(b se )  blankrow starlevels(10 5 1) bdec(4) noau keep(lnMW ) nolegend ///
  addrow("Elast", "[`elas']"\"""`obs'" ) ct("","(1)"   )

* foreach d of numlist 2 3 4 9 6 7 8 {
forvalues d = 2/8{


sum under [aw=wtsupp] if demgroup`d'==1
local DVmean=   `r(mean)'
di `DVmean'
estimates use "${est}ster/dube/MW1_`q'_dem`d'_s`spec'_dube"
  if `j' == 4  local obs : display %9.0gc `e(N)'
  else local obs
  local beta = _b[lnMW]
  di `beta'
  local elas:  di %5.4f `beta'/`DVmean'
  di `elas'
`append' addrow("Elast", "[`elas']"\"","`obs'") ct("","(`d')"   )

}


outreg, replay append(r)


}

outreg, replay
	outreg using "${est}table_dube_dem_a", statfont( fs12 ) basefont(fs12) replay replace landscape

	
**************************************************************************************

local append outreg, merge(r) starloc(1) var stats(b se) blankrow  merge starlevels(10 5 1) bdec(4) noau  keep(lnMW ) nolegend
local append1 outreg, merge(r) starloc(1) var stats(b se) blankrow  merge starlevels(10 5 1) bdec(4) noau   nolegend


local spec 4
local j .5
local d 1
* foreach d of numlist 2 3 4 5 6 {
*   use "${data1}regready_cps_ipums.dta", clear
* keep if dem`d'==1
outreg, clear
local thresholdlist 0.50(0.25)1.75
* local thresholdlist 0.50(0.25).75
local thresholdlist .5  1 1.5 2 3 4
* local thresholdlist .5  1 1.25 1.5 1.75 2 3 4
foreach j of numlist `thresholdlist' {
cap drop under
g  under = contpov < `j'
local q = `j' * 100
di "Poverty threshold `q'"
label var lnMW "INR<`j'"
local i 1
sum under [aw=wtsupp] if demgroup1==1
local DVmean=   `r(mean)'
di `DVmean'

* reghdfe under lnMWF1 lnMW lnMWL1 lnMWL2 lnMWL3 `primary'  [pw=fnlwgt] , a(`rhs`i'') cluster(state_fips)
* outreg, store(r)  starloc(1) replace var stats(b se )  blankrow starlevels(10 5 1) bdec(3) `stat' keep(tobacco21_treated  ) nolegend  addrow("","") ct("","(1)"   )
estimates use "${est}ster/dube/MW2_`q'_dem1_s`spec'_dube"
  if `j' == 4   local obs : display %11.0gc `e(N)'
*   di `obs'
  else local obs
  lincom lnMW +lnMWL1 +lnMWL2 + lnMWL3
  local beta = `r(estimate)'
  di `beta'
  local elas:  di %5.4f `beta'/`DVmean'
  di `elas'
  lincomest lnMW +lnMWL1 +lnMWL2  + lnMWL3
  mydisp
  outreg, store(r)  starloc(1) replace var stats(b se )  blankrow starlevels(10 5 1) bdec(4) noau nolegend ///
  addrow("Elast", "[`elas']"\"""`obs'" ) ct("","(1)"   )

forvalues d = 2/8{


sum under [aw=wtsupp] if demgroup`d'==1
local DVmean=   `r(mean)'
di `DVmean'
estimates use "${est}ster/dube/MW2_`q'_dem`d'_s`spec'_dube"
  if `j' == 4  local obs : display %9.0gc `e(N)'
  else local obs
  lincom lnMW +lnMWL1 +lnMWL2  + lnMWL3
  local beta = `r(estimate)'
  di `beta'
  local elas:  di %5.4f `beta'/`DVmean'
  di `elas'
  lincomest lnMW +lnMWL1 +lnMWL2  + lnMWL3
  mydisp
  `append1' addrow("Elast", "[`elas']"\"","`obs'") ct("","(`d')"   )
}


outreg, replay append(r)


}

outreg, replay
	outreg using "${est}table_dube_dem_b", statfont( fs12 ) basefont(fs12) replay replace landscape
